package com.example.lin.qq_home_project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lin.qq_home_project.R;
import com.example.lin.qq_home_project.pojo.Conversation;
import com.example.lin.qq_home_project.utils.DateUtils;

import org.w3c.dom.Text;

public class ConversationAdapter extends BaseAdapter {


    private Conversation conversation;

    private Context mContext;

    public ConversationAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public Conversation getConversation() {
        return conversation;
    }

    public void setConversation(Conversation conversation) {
        this.conversation = conversation;
        notifyDataSetChanged();//刷新适配器
    }

    @Override
    public int getCount() {
        return conversation == null ? 0 : conversation.getData().getResults().size();
    }

    @Override
    public Conversation.DataBean.ResultsBean getItem(int position) {
        return conversation.getData().getResults().get(position);
    }

    private class ViewHolder {

        ImageView iv_icon;
        TextView tv_title;
        TextView tv_lastMsg;
        TextView tv_time;

        public ViewHolder(View layout) {
            iv_icon = layout.findViewById(R.id.iv_icon);
            tv_title = layout.findViewById(R.id.tv_name);
            tv_lastMsg = layout.findViewById(R.id.tv_lastMsg);
            tv_time = layout.findViewById(R.id.tv_time);
            layout.setTag(this);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_wechat_message_list_item_1, parent, false);
            new ViewHolder(convertView);
        }
        ViewHolder vh = (ViewHolder) convertView.getTag();
        Conversation.DataBean.ResultsBean item = getItem(position);

        Glide.with(mContext).load(item.getUser().getUserImageUrl()).into(vh.iv_icon);
        vh.tv_title.setText(item.getEventName());
        vh.tv_lastMsg.setText(item.getEventDescription());
        String chatTime = DateUtils.getWeChatTime(item.getCreateTime());
        vh.tv_time.setText(chatTime);
        return convertView;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }
}
